home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bison.zip / GETOPT.C < prev    next >
C/C++ Source or Header  |  1989-02-03  |  3KB  |  88 lines

  1. /*
  2.  * Copyright (c) 1987 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #if defined(LIBC_SCCS) && !defined(lint)
  19. static char sccsid[] = "@(#)getopt.c    4.7 (Berkeley) 6/27/88";
  20. #endif /* LIBC_SCCS and not lint */
  21.  
  22. #include <stdio.h>
  23. #ifndef BSD
  24. # define index strchr
  25. #endif
  26.  
  27. /*
  28.  * get option letter from argument vector
  29.  */
  30. int    opterr = 1,        /* if error message should be printed */
  31.     optind = 1,        /* index into parent argv vector */
  32.     optopt;            /* character checked for validity */
  33. char    *optarg;        /* argument associated with option */
  34.  
  35. #define    BADCH    (int)'?'
  36. #define    EMSG    ""
  37. #define    tell(s)    { \
  38.     if (opterr) { \
  39.         fputs(*nargv, stderr); \
  40.         fputs(s, stderr); \
  41.         fputc(optopt, stderr); \
  42.         fputc((int)'\n', stderr); \
  43.     } \
  44.     return(BADCH); \
  45. }
  46.  
  47. getopt(nargc, nargv, ostr)
  48.     int nargc;
  49.     char **nargv, *ostr;
  50. {
  51.     static char *place = EMSG;        /* option letter processing */
  52.     register char *oli;            /* option letter list index */
  53.     char *index();
  54.  
  55.     if (!*place) {                /* update scanning pointer */
  56.         if (optind >= nargc || *(place = nargv[optind]) != '-')
  57.             return(EOF);
  58.         if (place[1] && *++place == '-') {    /* found "--" */
  59.             ++optind;
  60.             return(EOF);
  61.         }
  62.     }                    /* option letter okay? */
  63.     if ((optopt = (int)*place++) == (int)':' ||
  64.         !(oli = index(ostr, optopt))) {
  65.         if (!*place)
  66.             ++optind;
  67.         tell(": illegal option -- ");
  68.     }
  69.     if (*++oli != ':') {            /* don't need argument */
  70.         optarg = NULL;
  71.         if (!*place)
  72.             ++optind;
  73.     }
  74.     else {                    /* need an argument */
  75.         if (*place)            /* no white space */
  76.             optarg = place;
  77.         else if (nargc <= ++optind) {    /* no arg */
  78.             place = EMSG;
  79.             tell(": option requires an argument -- ");
  80.         }
  81.          else                /* white space */
  82.             optarg = nargv[optind];
  83.         place = EMSG;
  84.         ++optind;
  85.     }
  86.     return(optopt);                /* dump back option letter */
  87. }
  88.